package com.fang.transfer.file.client.thread;

import com.fang.transfer.file.client.AppContext;
import com.fang.transfer.file.client.connection.UdpUtil;
import com.fang.transfer.file.client.transfer.UdpService;
import com.fang.transfer.file.common.code.ClientSendCode;
import com.fang.transfer.file.common.model.TransferFileRequest;
import com.fang.transfer.file.common.model.User;
import com.google.gson.Gson;

/**
 * udp 打洞
 */
public class UDPHoleService implements Runnable {

    private TransferFileRequest request;

    private UdpService udpService;

    public UDPHoleService(TransferFileRequest request) {
        this.request = request;
        this.request.setStatus(3);
    }

    @Override
    public void run() {
        try {
            Thread.currentThread().setName("UDPHoleService");
            doRun();
        } catch (InterruptedException exception) {
            System.out.println("结束打洞");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void doRun() throws Exception {
        udpService = AppContext.udpServiceMap.get(request.getId());
        udpService.initSendUserAddr();
        while (request.getStatus() == 3) {
            System.out.println("UDP 打洞");
            udpService.holeSend();
            Thread.sleep(1000L);
        }
    }
}
